package com.ruoyi.system.service.project.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.project.book.Book;
import com.ruoyi.system.domain.project.book.BookTeacher;
import com.ruoyi.system.mapper.project.BookMapper;
import com.ruoyi.system.mapper.project.BookTeacherMapper;
import com.ruoyi.system.service.project.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 专著Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-30
 */
@Service
public class BookServiceImpl implements IBookService
{
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BookTeacherMapper bookTeacherMapper;
    /**
     * 查询专著
     * 
     * @param id 专著主键
     * @return 专著
     */
    @Override
    public Book selectBookById(Long id)
    {
        Book book = bookMapper.selectBookById(id);
        BookTeacher bookTeacher = new BookTeacher();
        bookTeacher.setBookId(book.getId());
        List<BookTeacher> bookTeachers = bookTeacherMapper.selectBookTeacherList(bookTeacher);
        book.setTeachers(bookTeachers);
        return book;
    }

    /**
     * 查询专著列表
     * 
     * @param book 专著
     * @return 专著
     */
    @Override
    public List<Book> selectBookList(Book book)
    {
        //如果是老师，则只显示自己上传的
        if(book.getCurrentRole()!=null&&book.getCurrentRole().equals("teacher")){
            book.setAuthorId(getUserId());
        }
        //如果是部门，则显示老师是系里的所有
        if(book.getCurrentRole()!=null&&book.getCurrentRole().equals("department")){
            book.setDeptId(SecurityUtils.getDeptId());
        }
        List<Book> list = bookMapper.selectBookList(book);
        for (Book book1 : list){
            BookTeacher bookTeacher = new BookTeacher();
            bookTeacher.setBookId(book1.getId());
            List<BookTeacher> bookTeachers = bookTeacherMapper.selectBookTeacherList(bookTeacher);
            book1.setTeachers(bookTeachers);
        }
        return  list;
    }

    /**
     * 新增专著
     * 
     * @param book 专著
     * @return 结果
     */
    @Override
//    @Caching(evict = {
//            @CacheEvict(
//                    value = "teacherStatistics_getIndexInfo",
//                    key = "#book.authorId+ '-' + T(java.time.format.DateTimeFormatter).ofPattern('yyyy').format(T(java.time.Instant).ofEpochMilli(#book.publicationReprintDate.time).atZone(T(java.time.ZoneId).systemDefault()).toLocalDate())"
//            ),
//            @CacheEvict(
//                    value = "departmentStatistics_getIndexInfo",
//                    key = "T(com.ruoyi.common.utils.SecurityUtils).getDeptId() + '-' + T(java.time.format.DateTimeFormatter).ofPattern('yyyy').format(T(java.time.Instant).ofEpochMilli(#book.publicationReprintDate.time).atZone(T(java.time.ZoneId).systemDefault()).toLocalDate())"
//            )
//    })
    public int insertBook(Book book)
    {
        if(book.getId()!=null){
            BookTeacher bookTeacher = new BookTeacher();
            bookTeacher.setBookId(book.getId());
            bookTeacher.setUserId(book.getAuthorId().toString());
            bookTeacher.setUserName(book.getMainAuthorsTranslators());
            bookTeacher.setRank(book.getRank());
            bookTeacher.setMyWord(book.getMyWord());
            bookTeacherMapper.insertBookTeacher(bookTeacher);
        }else {
            int result = bookMapper.insertBook(book);
            if(result>0){
                BookTeacher bookTeacher = new BookTeacher();
                bookTeacher.setBookId(book.getId());
                bookTeacher.setUserId(book.getAuthorId().toString());
                bookTeacher.setUserName(book.getMainAuthorsTranslators());
                bookTeacher.setRank(book.getRank());
                bookTeacher.setMyWord(book.getMyWord());
                bookTeacherMapper.insertBookTeacher(bookTeacher);
            }
        }
        return 1;
    }

    /**
     * 修改专著
     * 
     * @param book 专著
     * @return 结果
     */
    @Override
    public int updateBook(Book book)
    {
        return bookMapper.updateBook(book);
    }

    /**
     * 批量删除专著
     * 
     * @param ids 需要删除的专著主键
     * @return 结果
     */
    @Override
    public int deleteBookByIds(Long[] ids)
    {
        for (Long id : ids){
            BookTeacher bookTeacher = new BookTeacher();
            bookTeacher.setBookId(id);
            List<BookTeacher> bookTeachers = bookTeacherMapper.selectBookTeacherList(bookTeacher);
            for (BookTeacher bookTeacher1 : bookTeachers){
                bookTeacherMapper.deleteBookTeacherById(bookTeacher1.getId());
            }
        }
        return bookMapper.deleteBookByIds(ids);
    }

    /**
     * 删除专著信息
     * 
     * @param id 专著主键
     * @return 结果
     */
    @Override
    public int deleteBookById(Long id)
    {
        return bookMapper.deleteBookById(id);
    }
    /**
     * 通过标题查询专著信息
     *
     * @param data 专著标题
     * @return 专著信息
     */
    @Override
    public Book getBookInfoByTitle(String data) {
        return bookMapper.getBookInfoByTitle( data);
    }
}
